﻿using DAL.Interfejsi;
using MySql.Data.MySqlClient;
using sistem;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    partial class DAL
    {
        public class KabinetDAO : IDaoCrud<Kabinet>
        {
            #region IDaoCrud<Kabinet> Members
            protected MySqlCommand c;
            public long create(Kabinet entity)
            {
                try
                {
                    c = new MySqlCommand("insert into kabinet (naziv, sprat, kapacitet, slika) values (@Naziv, @Sprat, @Kapacitet, @Slika);", con);
                    c.Parameters.AddWithValue("@Naziv", entity.Naziv);
                    c.Parameters.AddWithValue("@Sprat", entity.Sprat);
                    c.Parameters.AddWithValue("@Kapacitet", entity.Kapacitet);
                    c.Parameters.AddWithValue("@Slika", entity.Slika);
                    c.ExecuteNonQuery();
                    return c.LastInsertedId;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Kabinet> getAll()
            {
                try
                {
                    c = new MySqlCommand("select * from kabinet;", con);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Kabinet> kabineti = new List<Kabinet>();
                    while (r.Read())
                    {
                        Kabinet kabinet = new Kabinet();
                        kabinet.ID = r.GetInt32("id");
                        kabinet.Naziv = r.GetString("naziv");
                        kabinet.Sprat = r.GetInt32("sprat");
                        kabinet.Kapacitet = r.GetInt32("kapacitet");
                        kabinet.Slika = r.GetString("slika");
                        kabineti.Add(kabinet);
                    }
                    return kabineti;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Kabinet read(Kabinet entity)
            {
                try
                {
                    c = new MySqlCommand("select * from kabinet where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    MySqlDataReader r = c.ExecuteReader();
                    Kabinet kabinet = new Kabinet();
                    if (r.Read())
                    {
                        kabinet.ID = r.GetInt32("id");
                        kabinet.Naziv = r.GetString("naziv");
                        kabinet.Sprat = r.GetInt32("sprat");
                        kabinet.Kapacitet = r.GetInt32("kapacitet");
                        kabinet.Slika = r.GetString("slika");
                    }
                    return kabinet;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Kabinet update(Kabinet entity)
            {
                try
                {
                    c = new MySqlCommand("UPDATE kabinet SET naziv = @Naziv, sprat = @Sprat, kapacitet = @Kapacitet, slika = @Slika where id = @ID;", con);
                    c.Parameters.AddWithValue("@Naziv", entity.Naziv);
                    c.Parameters.AddWithValue("@Sprat", entity.Sprat);
                    c.Parameters.AddWithValue("@Kapacitet", entity.Kapacitet);
                    c.Parameters.AddWithValue("@Slika", entity.Slika);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                    return entity;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public void delete(Kabinet entity)
            {
                try
                {
                    c = new MySqlCommand("delete from kabinet where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", entity.ID);
                    c.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public Kabinet getById(int id)
            {
                try
                {
                    c = new MySqlCommand("select * from kabinet where id = @ID;", con);
                    c.Parameters.AddWithValue("@ID", id);
                    MySqlDataReader r = c.ExecuteReader();
                    Kabinet kabinet = new Kabinet();
                    if (r.Read())
                    {
                        kabinet.ID = r.GetInt32("id");
                        kabinet.Naziv = r.GetString("naziv");
                        kabinet.Sprat = r.GetInt32("sprat");
                        kabinet.Kapacitet = r.GetInt32("kapacitet");
                        kabinet.Slika = r.GetString("slika");
                    }
                    return kabinet;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }

            public List<Kabinet> getByExample(string name, string value)
            {
                try
                {
                    c = new MySqlCommand("select * from kabinet where @Kolona = @Vrijednost;", con);
                    c.Parameters.AddWithValue("@Kolona", name);
                    c.Parameters.AddWithValue("@Vrijednost", value);
                    MySqlDataReader r = c.ExecuteReader();
                    List<Kabinet> kabineti = new List<Kabinet>();
                    while (r.Read())
                    {
                        Kabinet kabinet = new Kabinet();
                        kabinet.ID = r.GetInt32("id");
                        kabinet.Naziv = r.GetString("naziv");
                        kabinet.Sprat = r.GetInt32("sprat");
                        kabinet.Kapacitet = r.GetInt32("kapacitet");
                        kabinet.Slika = r.GetString("slika");
                        kabineti.Add(kabinet);
                    }
                    return kabineti;
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            #endregion
        }
    }
}
